<?php
namespace Home\Controller;
use Think\Controller;
class UserController extends Controller {
	
	/**
	 * 魔术方法，设置默认处理机制
	 * @param String $method
	 * @param String $args
	 */
	public function __call($method, $args){
		if (method_exists('UserController', $method)){
			$this->$method;
		}else{
			$response['STATUS'] = "error";
			$response['ERRMSG'] = "no method";
			$response['RESULT'] = array();
			$this->ajaxReturn($response);
		}
	}

    public function checkRegister(){

		$this->assign('WEB_ROOT',C('WEB_ROOT'));
		$user = cookie("jspz");//用户名
		if(!empty($user)){
			$this->redirect('Index/index');
		}
		$this->display('register');return;
		
	 }

	 public function toLogin(){
		$this->assign('WEB_ROOT',C('WEB_ROOT'));
		$user = cookie("jspz");//用户名
		if(empty($user)){
			$this->display('register');return;
		}
		//$this->theme('index')->display('index');
		$this->redirect('Product/getProduct', array('id' => 1));
	 }

	 public function toLogin2(){
		$this->assign('WEB_ROOT',C('WEB_ROOT'));
		$user = cookie("jspz");//用户名
		if(empty($user)){
			$this->display('login');return;
		}
		//$this->theme('index')->display('index');
		$this->redirect('Product/getProduct', array('id' => 1));
	 }

	 public function login(){
	 	
		$this->assign('WEB_ROOT',C('WEB_ROOT'));
		$user = cookie("jspz");//用户名
		if(!empty($user)){
			$response['STATUS']="error";
			$response['MSG']="用户已登录";
			$response['RESULT'] = array("type"=>0);
			$this->ajaxReturn($response);
		}
		$phone = I('param.tel');
		$pass = I('param.password');
		if(empty($phone) || empty($pass)){
			$response['STATUS']="error";
			$response['MSG']="手机号密码不能为空";
			$response['RESULT'] = array("type"=>1);
			$this->ajaxReturn($response);
			// $this->error('手机号或密码不能为空');return;
		}

		$res=M('users')->where(array('phone'=>$phone))->find();

	 	if(!empty($res)&&$res['password']==md5($pass)){
	 		cookie('jspz',$res['phone']);
	 		
	 		$temp_pid=cookie("temp_pid");
			$temp_sway=cookie("temp_sway");
			$temp_area=cookie("temp_area");
			$temp_type=cookie("temp_type");
	 		$response['STATUS']="success";
			$response['MSG']="登录成功，跳转首页";
			$response['RESULT'] = array("temp_pid"=>$temp_pid,
				"temp_sway"=>$temp_sway,"temp_area"=>$temp_area,"temp_type"=>$temp_type);
			$this->ajaxReturn($response);
	 		
	 	}
	 	$response['STATUS']="error";
		$response['MSG']="账号密码错误";
		$response['RESULT'] = array("type"=>2);
		$this->ajaxReturn($response);
	 }
	 
	 public function checkPwd(){
		$phone = I('param.tel');
		$pass = I('param.password');
		$res=M('users')->where(array('phone'=>$phone))->find();
	 	if(!empty($res)&&$res['password']==md5($pass)){
	 		$this->show("ok");
	 	}else{
	 		$this->show("failed");
	 	}
	 	
	 }	 

	 //提交注册信息
	 public function register(){
	 	//校验验证码 start
	 	$code_confirm=cookie('code');
	 	$code = I('param.code');

	 	if(empty($code) || $code != $code_confirm){
	 		$response['STATUS']="error";
			$response['MSG']="验证码错误";
			$response['RESULT'] = array("type"=>1);
			$this->ajaxReturn($response);
	 	}
	 	//校验验证码 end
	 	$users['name']=I('param.realname');
	 	$users['phone']=I('param.tel');
	 	$olduser=M('users')->where(array('phone'=>$users['phone']))->find();
//	 	$users['email']=I('param.email');
	 	$users['password']=md5(I('param.password'));
	 	$users['create_at']=date("Y-m-d h:i:s");
	 	if(empty($olduser)){
	 		$res=M('users')->add($users);
	 		
	 	}else{
	 		$olduser['name']=$users['name'];
	 		$olduser['phone']=$users['phone'];
	 		$olduser['password']=$users['password'];
	 		$olduser['update_at']=date("Y-m-d h:i:s");
	 		$res=M('users')->where(array('phone'=>$users['phone']))->save($olduser);
	 	}
	 	if(empty($res)){
	 		$response['STATUS']="error";
			$response['MSG']="注册失败";
			$response['RESULT'] = array("type"=>2);
			$this->ajaxReturn($response);
	 	}else{
	 		cookie('jspz',$users['phone']);
	 		$response['STATUS']="success";
			$temp_pid=cookie("temp_pid");
			$temp_sway=cookie("temp_sway");
			$temp_area=cookie("temp_area");
			$temp_type=cookie("temp_type");
	 		$response['STATUS']="success";
			$response['MSG']="注册成功，跳转首页";
			$response['RESULT'] = array("temp_pid"=>$temp_pid,
				"temp_sway"=>$temp_sway,"temp_area"=>$temp_area,"temp_type"=>$temp_type);
			$this->ajaxReturn($response);	
	 	}
	 }

	 //获取验证码
	 public function getCode(){
	 	$phone = I('param.phone');
	 	if (!empty($phone)) {
	 		// $res=M('users')->where(array("phone"=>$phone))->find();
	 		// //不存在可以注册
	 		// if(empty($res)){
	 		// 	return $this->sendMsg($phone);
	 		// }else{
	 		// 	$this->error("用户已注册");
	 		// }
	 		return $this->sendMsg($phone);
	 	}else{
	 		return false;
	 	}
		
	 }

	 public function loginOut(){
		cookie("jspz",null);//用户名
		
		$this->redirect('Index/index');
	 }
	 /**
	 * 发送短信方法
	 * @param  $phone 目标电话
	 * @param  $msg	信息内容
	 * @return boolean
	 */
	public function sendMsg($phone){
		$code = rand(1000,9999);
		$msg = '验证码:'.$code;
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, "http://sms-api.luosimao.com/v1/send.json");
		curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
		curl_setopt($ch, CURLOPT_HEADER, FALSE);
		curl_setopt($ch, CURLOPT_HTTPAUTH , CURLAUTH_BASIC);
		curl_setopt($ch, CURLOPT_USERPWD  , 'api:key-cc801eb0ce1819ab5966c22a2e1b10b1');
		curl_setopt($ch, CURLOPT_POST, TRUE);
		curl_setopt($ch, CURLOPT_POSTFIELDS, array("mobile"=>$phone,"message"=>$msg."【极速牌照】"));
		$res = curl_exec( $ch );
		curl_close( $ch );
		
		$response = json_decode($res);
		if($response->msg == 'ok'){
			//session(array('code'=>$code,'expire'=>3600));
			cookie("code",$code);
		}
		return $response->msg == 'ok';
	}
    //public function blog(){
    //    $this->assign('WEB_ROOT',C('WEB_ROOT'));
    //    $this->display("blog");
    //}
    //public function singleblog(){
    //    $this->assign('WEB_ROOT',C('WEB_ROOT'));
    //    $this->display("single-blog");
    //}
}
